package com.truongnha.clienttn.business.service;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.Log;
import com.truongnha.clienttn.business.model.StudentMarkModel;
import com.truongnha.clienttn.data.dao.CreateDatabase;
import com.truongnha.clienttn.data.dao.StudentMarkDAO;
import com.truongnha.clienttn.data.dao.StudentSubjectDAO;
import com.truongnha.clienttn.data.entity.StudentMarkEntity;
import com.truongnha.clienttn.data.entity.StudentSubjectEntity;
import com.truongnha.clienttn.data.sao.StudentMarkSAO;

public class StudentMarkByTimeService {
	Context context;

	public StudentMarkByTimeService(Context context) {
		super();
		this.context = context;
	}

	public void getMarkFromServer() {
		StudentMarkSAO studentMarkSAO = new StudentMarkSAO(context);
		studentMarkSAO.getMarkFromServer();
	}

	public void getAll(List<StudentMarkModel> list) {

		HashMap<Integer, String> hmSubjectName = new HashMap<Integer, String>();
		try {
			CreateDatabase db = new CreateDatabase(context);
			// get list subject
			StudentSubjectDAO studentSubjectDAO = new StudentSubjectDAO(
					db.open());
			List<StudentSubjectEntity> listStudentSubject = studentSubjectDAO
					.getAll();
			for (StudentSubjectEntity studentSubjectEntity : listStudentSubject) {
				hmSubjectName.put(studentSubjectEntity.getMarkId(),
						studentSubjectEntity.getName());
			}
			db.close();
		} catch (Exception e) {
			Log.e("log error", "error in student mark service 1" + e.toString()
					+ e.getStackTrace().toString());
		}

		try {
			CreateDatabase db = new CreateDatabase(context);
			StudentMarkDAO studentMarkDAO = new StudentMarkDAO(db.open());
			List<StudentMarkEntity> listMarkEntity = studentMarkDAO.getByTime();
			for (StudentMarkEntity studentMarkEntity : listMarkEntity) {
				list.add(new StudentMarkModel(studentMarkEntity, hmSubjectName
						.get(studentMarkEntity.getMark_id())));
			}

			db.close();
		} catch (Exception e) {
			Log.e("log error",
					"error in student mark service 2 " + e.toString()
							+ e.getStackTrace().toString());
		}
	}

}
